
var uc_objectHtml = '<object id="ucstarObj" width="0" height="0" classid="clsid:2532DED7-AB04-4D70-9D0C-1FB71F13323F" style="display:none"></object>';
var uc_objectCtrl = null;
var uc_isFirstLoad = false;
var uc_isObjectCtrlInited = false;
var uc_isObjectCtrlLoadError = false;
var uc_clientOnline = false;
var uc_oldWinOnload = null;
var uc_tryCount = 0;
var UC_TIMEOUT_COUNT = 10;

function uc_GetAbsolutePos(element) {
	if (arguments.length != 1 || element == null) {
		return null;
	}
	var elmt = element;
	var offsetTop = elmt.offsetTop;
	var offsetLeft = elmt.offsetLeft;
	var offsetWidth = elmt.offsetWidth;
	var offsetHeight = elmt.offsetHeight;
	while (elmt = elmt.offsetParent) {
		if (elmt.style.position == 'absolute' || elmt.style.position == 'relative' || (elmt.style.overflow != 'visible' && elmt.style.overflow != '')) {
			break;
		}
		offsetTop += elmt.offsetTop;
		offsetLeft += elmt.offsetLeft;
	}
	return { absoluteTop: offsetTop, absoluteLeft: offsetLeft, offsetWidth: offsetWidth, offsetHeight: offsetHeight };
}

function uc_winOnload()
{
	if( typeof(uc_oldWinOnload) == "function" )
	{
		uc_oldWinOnload();
	}
	
	uc_createObjectControl();
}

function uc_registerWinOnload() {
	if( !uc_isFirstLoad )
	{
		uc_isFirstLoad = true;
		
		uc_oldWinOnload = window.onload;
		window.onload = uc_winOnload;
	}
}

function uc_ensureObjectCtrl() {
	if ( !uc_isObjectCtrlInited ) {
		uc_isObjectCtrlInited = true;
		if ( uc_objectCtrl ) {
			
		}
	}
	return uc_objectCtrl;
}

function uc_TestTimeout() {
	if ( document.readyState == "complete" ) {
		document.body.insertAdjacentHTML("beforeEnd", uc_objectHtml);
		uc_objectCtrlLoadTimer();
	} else {
		window.setTimeout("uc_TestTimeout();", 100);
	}
}

function uc_createObjectControl() {
//	alert("document.readyState=" + document.readyState);
//	document.body.insertAdjacentHTML("beforeEnd", uc_objectHtml);
//	uc_objectCtrlLoadTimer();
	uc_TestTimeout();
}

function uc_objectCtrlLoadTimer()
{
	if( !uc_isObjectCtrlLoadError )
	{
		var obj = document.all("ucstarObj");
		if ( !obj ) {
			if ( uc_tryCount < UC_TIMEOUT_COUNT ) {
				uc_tryCount++;
				window.setTimeout("uc_objectCtrlLoadTimer();", 100);
			} else {
				uc_ensureObjectCtrl();
			}
		}
	} else {
		uc_ensureObjectCtrl();
	}
}

function uc_onStatusChange(status, comment) {
	//alert("status=" + status + ", comment=" + comment);
	uc_clientOnline = (status == "1");
}

function isUcstarOnline() {
	var o = new Object();
	if ( uc_clientOnline ) {
		o.isOnline = uc_clientOnline;
		o.user = uc_objectCtrl.GetLoginName();
		return o;
	}
	
	if ( uc_objectCtrl == null ) {
		uc_objectCtrl = document.getElementById("ucstarObj");
	//	alert("uc_objectCtrl=" + uc_objectCtrl);
	//	uc_objectCtrl.attachEvent("OnStatusChange", uc_onStatusChange);
	}

	var username = "";
//	alert("uc_objectCtrl=" + uc_objectCtrl);
	if ( uc_objectCtrl ) {
		try {
			var r = uc_objectCtrl.InitUc();
			if ( r == 0 ) {
				r = uc_objectCtrl.GetUcPresence();
				username = uc_objectCtrl.GetLoginName();
				uc_clientOnline = (r == 1);
			}
		} catch ( ex ) {
			//uc_clientOnline = false;
		}
	}

	o.isOnline = uc_clientOnline;
	o.user = username;
	
	return o;
}

function uc_updateImage(id, uc_gender, tryCount) {
	if ( tryCount == 0 ) {
		return;
	}
	
	var uc_image = document.getElementById(id);
	if ( uc_image ) {
		var o = isUcstarOnline();
		if ( o.isOnline ) {
			var uc_user = uc_image.id.replace(/^ucimg_[0-9]*_/, "");
			var imageSrc = "../../ucstar/images/";
			var uc_state = uc_objectCtrl.GetBatchState(uc_user);
			if (uc_gender == 0) {
				if (uc_state == 1) {
					imageSrc += "2-1.png";	
				} else {
					imageSrc += "2-2.png";
				}	
			} else if (uc_gender == 1) {
				if (uc_state == 1) {
					imageSrc += "1-1.png";	
				} else {
					imageSrc += "1-2.png";
				}
			}
		//	uc_image.style.filter = "Chroma(Color = '#00008080', Enable = true)";
			uc_image.src = imageSrc;
		} else {
			window.setTimeout("uc_updateImage('" + id + "', " + uc_gender + ", " + (tryCount - 1) + ");", 50);
		}
	}
}

function uc_CheckStatus(uc_image, uc_gender) {
//	uc_registerWinOnload();
	var o = document.getElementById("ucstarObj");
	if ( !o ) {
		uc_createObjectControl();
	}
	
	uc_image.onload = null;
	window.setTimeout("uc_updateImage('" + uc_image.id + "', " + uc_gender + ", 10);", 50);
}

function uc_sendMessage(username, oa_user) {
	var o = isUcstarOnline();
	if ( !o.isOnline ) {
		alert("无法发送消息，请先登录UcSTAR");
		return;
	}
	if ( o.user != oa_user ) {
		alert("登录UcSTAR用户与协同用户不一致");
		return;
	}
	try {
		var x = uc_objectCtrl.SendMessageTo("", 1, username);
	} catch ( ex ) {
		alert("发生异常：" + ex.message);
	}
}

function uc_sendEmail(username, oa_user) {
	var o = isUcstarOnline();
	if ( !o.isOnline ) {
		alert("无法发送邮件，请先登录UcSTAR");
		return;
	}
	if ( o.user != oa_user ) {
		alert("登录UcSTAR用户与协同用户不一致");
		return;
	}
	try {
		var x = uc_objectCtrl.SendEmail(username);
	} catch ( ex ) {
		alert("发生异常：" + ex.message);
	}
}

function uc_sendSms(username, oa_user) {
	var o = isUcstarOnline();
	if ( !o.isOnline ) {
		alert("无法发送短信，请先登录UcSTAR");
		return;
	}
	if ( o.user != oa_user ) {
		alert("登录UcSTAR用户与协同用户不一致");
		return;
	}
	try {
		var x = uc_objectCtrl.SendSms(username);
	} catch ( ex ) {
		alert("发生异常：" + ex.message);
	}
}

function uc_MouseOver(src, id) {
	var obj = document.getElementById(src);
	var pos = uc_GetAbsolutePos(obj);
	var suffix = id.replace(/^ucmenu_/, "");
	var user = id.replace(/^ucmenu_[0-9]*_/, "");
	var o = isUcstarOnline();
	if ( user == o.user ) {
		return;
	}
	
//	alert(user);
	var menu = document.getElementById(id);
	if ( menu ) {
		menu.style.offsetLeft = pos.absoluteLeft;
		menu.style.offsetTop = pos.absoluteTop + pos.offsetHeight;
		menu.style.display = "";
	}
}

function uc_MouseOut(id) {
	var menu = document.getElementById(id);
	if ( menu ) {
		menu.style.display = "none";
	}
}
